package com.ruoyi.web.controller.estate.emergency;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.estate.emergency.EmergencySuppliesStock;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.estate.emergency.EmergencySupplies;
import com.ruoyi.system.service.estate.emergency.IEmergencySuppliesService;
import com.ruoyi.system.service.estate.emergency.IEmergencySuppliesStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 应急物资Controller
 */
@RestController
@RequestMapping("/estate/emergency/supplies")
public class EmergencySuppliesController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(EmergencySuppliesController.class);

    @Autowired
    private IEmergencySuppliesService emergencySuppliesService;

    @Autowired
    private IEmergencySuppliesStockService emergencySuppliesStockService;

    /**
     * 查询应急物资列表
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmergencySupplies emergencySupplies) {
        startPage();
        List<EmergencySupplies> list = emergencySuppliesService.selectEmergencySuppliesList(emergencySupplies);
        return getDataTable(list);
    }

    /**
     * 导出应急物资列表
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:export')")
    @Log(title = "应急物资", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EmergencySupplies emergencySupplies) {
        List<EmergencySupplies> list = emergencySuppliesService.selectEmergencySuppliesList(emergencySupplies);
        ExcelUtil<EmergencySupplies> util = new ExcelUtil<EmergencySupplies>(EmergencySupplies.class);
        return util.exportExcel(list, "应急物资数据");
    }

    /**
     * 获取应急物资详细信息
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:query')")
    @GetMapping(value = "/{suppliesId}")
    public AjaxResult getInfo(@PathVariable("suppliesId") Long suppliesId) {
        return success(emergencySuppliesService.selectEmergencySuppliesById(suppliesId));
    }

    /**
     * 新增应急物资
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:add')")
    @Log(title = "应急物资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmergencySupplies emergencySupplies) {
        return toAjax(emergencySuppliesService.insertEmergencySupplies(emergencySupplies));
    }

    /**
     * 修改应急物资
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:edit')")
    @Log(title = "应急物资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmergencySupplies emergencySupplies) {
        return toAjax(emergencySuppliesService.updateEmergencySupplies(emergencySupplies));
    }

    /**
     * 删除应急物资
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:supplies:remove')")
    @Log(title = "应急物资", businessType = BusinessType.DELETE)
    @DeleteMapping("/{suppliesIds}")
    public AjaxResult remove(@PathVariable Long[] suppliesIds) {
        return toAjax(emergencySuppliesService.deleteEmergencySuppliesByIds(suppliesIds));
    }
    
    /**
     * 获取应急物资统计信息
     */
    @GetMapping("/statistics/info")
    public AjaxResult getStatistics() {
        Map<String, Object> statistics = emergencySuppliesService.selectEmergencySuppliesStatistics();
        return success(statistics);
    }
    
    /**
     * 获取应急物资类型统计
     */
    @GetMapping("/type/statistics/list")
    public AjaxResult getTypeStatistics() {
        List<Map<String, Object>> typeStatistics = emergencySuppliesService.selectEmergencySuppliesTypeStatistics();
        return success(typeStatistics);
    }

    /**
     * 获取即将过期物资列表
     */
    @GetMapping("/expiring/list")
    public AjaxResult getExpiringSupplies() {
        List<EmergencySuppliesStock> expiringSupplies = emergencySuppliesStockService.selectExpiringSupplies();
        logger.info("查询到即将过期物资数量: {}", expiringSupplies.size());
        if (!expiringSupplies.isEmpty()) {
            logger.info("第一条记录: {}", expiringSupplies.get(0));
        }
        return success(expiringSupplies);
    }

    /**
     * 获取应急物资分布情况
     */
    @GetMapping("/distribution/list")
    public AjaxResult getSuppliesDistribution() {
        List<Map<String, Object>> distribution = emergencySuppliesStockService.selectStockDistribution();
        return success(distribution);
    }

    /**
     * 获取物资选项列表
     */
    @GetMapping("/options")
    public AjaxResult getSuppliesOptions() {
        EmergencySupplies query = new EmergencySupplies();
        query.setStatus("0"); // 只查询正常状态的物资
        List<EmergencySupplies> list = emergencySuppliesService.selectEmergencySuppliesList(query);
        return success(list);
    }

    /**
     * 获取存放位置选项列表
     */
    @GetMapping("/location/options")
    public AjaxResult getLocationOptions() {
        List<SysDictData> dictDataList = DictUtils.getDictCache("emergency_supplies_location");
        return success(dictDataList);
    }
} 